/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlleGruppeeAnzeigenGUI.java
 *
 * Created on 20.05.2011, 22:39:53
 */
package sixsigma;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author SS
 */
public class GruppenverwaltungGUI extends javax.swing.JFrame {
    static int groupId;
    DbKomm dbk = new DbKomm();
    
    //BEGIN - Klasseninterne Methoden
    public String[][] getData(String SQL){
        try {dbk.open();} catch(Exception ex){Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);}
        String[][] ergebnis = dbk.einfachArrayAuslesen(SQL);
        try {dbk.close();} catch(Exception ex){Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);}
        return ergebnis;
    }

    public void setData(String SQL){
        try {dbk.open();} catch(Exception ex){Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);}
        dbk.execute(SQL);
        try {dbk.close();} catch(Exception ex){Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);}
    }
    //END - Klasseninterne Methoden

    /** Creates new form AlleGruppeeAnzeigenGUI */
    public GruppenverwaltungGUI() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(GruppenverwaltungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        super.setLocationRelativeTo(null);        

        jComboBox1.removeAllItems();
        String [][] gs;
        if(jComboBox2.getSelectedItem().toString().equals("FR")){
            gs = getData("SELECT FR_GROUP_ID, FR_GROUP_TEXT FROM fr_group");
        }else{
            gs = getData("SELECT DP_GROUP_ID, DP_GROUP_TXT FROM dp_group");
        }
        
        for (int i=0; i<gs.length; i++){
            if (!gs[i][0].isEmpty()) {
                jComboBox1.addItem(new Item(Integer.parseInt(gs[i][0]), gs[i][1]));
            }
        }
        
        Item item = (Item)jComboBox1.getSelectedItem();
        jLabel4.setText("Gruppe "+item.getDescription());
        jLabel3.setText("Name von Gruppe "+item.getDescription());
    }
    
    class Item  
    {  
        private int id;  
        private String description;  
   
        public Item(int id, String description)  
        {  
            this.id = id;  
            this.description = description;  
        }  
   
        public int getId()  
        {  
            return id;  
        }  
   
        public String getDescription()  
        {  
            return description;  
        }  
   
        public String toString()  
        {  
            return description;  
        }  
    }  

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Gruppe anlegen"));

        jButton1.setText("anlegen");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Gruppenname");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Gruppe ändern / löschen"));

        jLabel2.setText("Gruppe auswählen");

        jButton2.setText("löschen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("ändern");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Gruppezugehörigkeit"));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DP", "FR" }));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jComboBox2, 0, 357, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Neue Gruppe anlegen
        if(jComboBox2.getSelectedItem().toString().equals("FR")){
            if (Character.isDigit(jTextField1.getText().charAt(0))|jTextField1.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,"Gruppenname kann nicht mit einer Nummer beginnen und darf nicht leer sein!");
            } else {
            setData("INSERT INTO fr_group (FR_GROUP_TEXT) VALUES ('"+jTextField1.getText()+"')");
            JOptionPane.showMessageDialog(null,"Gruppe erfolgreich angelegt!");
            jTextField1.setText("");
            refresh();
            }
        }
        if(jComboBox2.getSelectedItem().toString().equals("DP")){
            if (Character.isDigit(jTextField1.getText().charAt(0))|jTextField1.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,"Gruppenname kann nicht mit einer Nummer beginnen und darf nicht leer sein!");
            } else {
            setData("INSERT INTO dp_group (DP_GROUP_TXT) VALUES ('"+jTextField1.getText()+"')");
            JOptionPane.showMessageDialog(null,"Gruppe erfolgreich angelegt!");
            jTextField1.setText("");
            refresh();
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //Gruppe löschen
        if(jComboBox2.getSelectedItem().toString().equals("DP")){
            setData("DELETE FROM dp_group WHERE DP_GROUP_ID ="+GruppenverwaltungGUI.groupId);
            JOptionPane.showMessageDialog(null,"Gruppe erfolgreich gelöscht!");
            refresh(); 
        }
        if(jComboBox2.getSelectedItem().toString().equals("FR")){
            setData("DELETE FROM fr_group WHERE FR_GROUP_ID ="+GruppenverwaltungGUI.groupId);
            JOptionPane.showMessageDialog(null,"Gruppe erfolgreich gelöscht!");
            refresh();
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //Gruppennamen ändern
        if(jComboBox2.getSelectedItem().toString().equals("DP")){
            if (Character.isDigit(jTextField2.getText().charAt(0))|jTextField2.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,"Gruppenname kann nicht mit einer Nummer beginnen und darf nicht leer sein!");
            } else {
            setData("UPDATE dp_group SET DP_GROUP_TXT = '"+jTextField2.getText()+"' WHERE DP_GROUP_ID ="+GruppenverwaltungGUI.groupId);
            JOptionPane.showMessageDialog(null,"Gruppen erfolgreich geändert!");
            jTextField2.setText("");
            refresh();
            }
        }
        if(jComboBox2.getSelectedItem().toString().equals("FR")){
            if (Character.isDigit(jTextField2.getText().charAt(0))|jTextField2.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,"Gruppenname kann nicht mit einer Nummer beginnen und darf nicht leer sein!");
            } else {
            setData("UPDATE fr_group SET FR_GROUP_TEXT = '"+jTextField2.getText()+"' WHERE FR_GROUP_ID ="+GruppenverwaltungGUI.groupId);
            JOptionPane.showMessageDialog(null,"Gruppen erfolgreich geändert!");
            jTextField2.setText("");
            refresh();
            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        Item item = (Item)jComboBox1.getSelectedItem();  
        if (jComboBox1.getItemCount()>0) {
            this.groupId = item.getId();        
            jLabel4.setText("Gruppe "+item.getDescription());
            jLabel3.setText("Name von Gruppe "+item.getDescription());
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
        refresh();
    }//GEN-LAST:event_jComboBox2ItemStateChanged

    private void refresh(){
        jComboBox1.removeAllItems();
        if(jComboBox2.getSelectedItem().toString().equals("DP")){
            String [][] gs = getData("SELECT DP_GROUP_TXT, DP_GROUP_ID FROM dp_group");
            for (int i=0; i<gs.length; i++){
                jComboBox1.addItem(new Item(Integer.parseInt(gs[i][1]), gs[i][0]));
            }
        }
        if(jComboBox2.getSelectedItem().toString().equals("FR")){
            String [][] gs = getData("SELECT FR_GROUP_TEXT,FR_GROUP_ID FROM fr_group");
            for (int i=0; i<gs.length; i++){
                jComboBox1.addItem(new Item(Integer.parseInt(gs[i][1]), gs[i][0]));
            }   
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
